#打印购物小条，内容为物品名、花费金额、总金额
#定义购物函数，返回购物小条
#购物券功能（建立购物券字典），用户购物券字典包含购物券张数，购物券
#退货功能(用户得有这个商品，建立一个用户现有商品的字典）
import random


#购物函数
def shopping(shopping_trolley_form, remaining_money, commodity_form, discount_customer_form, commodity_inventory_form):

    sum_money = 0  #计算总价格
    for item in shopping_trolley_form:
        mark = 0  #判断是否已用优惠券购买
        if item in commodity_form:
            print(f"商场里有{item}这个商品")
            for discount_form in discount_customer_form:  #顺序查找对应商品的优惠券，如果遇到直接扣钱退出循环
                if discount_customer_form[discount_form] == 1 and discount_form[0] == item:  #找到商品对应的优惠券
                    print("找到优惠券")
                    discount_money = commodity_form[item] * discount_form[1]
                    if discount_money <= remaining_money:
                        sum_money += discount_money
                        remaining_money -= discount_money
                        print(f"{item}购买成功")
                        discount_customer_form[discount_form] -= 1
                        mark = 1
                        if item not in commodity_inventory_form:
                            commodity_inventory_form[item] = 0
                        commodity_inventory_form[item] += 1
                        break
                    else:
                        print(f"{item}购买失败，穷鬼，钱不够！请到其它超市购买！")
                        break

            if commodity_form[item] <= remaining_money:
                if mark == 0:
                    sum_money += commodity_form[item]
                    remaining_money -= commodity_form[item]  #扣钱
                    print(f"{item}购买成功！")
                    if item not in commodity_inventory_form:
                        commodity_inventory_form[item] = 0
                    commodity_inventory_form[item] += 1
            else:
                print(f"{item}购买失败，穷鬼，钱不够！请到其它超市购买！")
                break
        else:
            print(f"没有{item}这个商品，别瞎弄！")


    return commodity_inventory_form, remaining_money, sum_money



#购物券函数，返回用户现在有的购物券清单
def coupon(discount_customer_form):

    #抽折扣券
    discount_list = list(discount_customer_form.keys())
    random_discount = random.choice(discount_list)
    print(f"抽取到的优惠券为{random_discount}")
    #折扣券添加
    if random_discount == ("car", 0.5):
        discount_customer_form[("car", 0.5)] += 1
    elif random_discount == ("car", 0.6):
        discount_customer_form[("car", 0.6)] += 1
    elif random_discount == ("car", 0.7):
        discount_customer_form[("car", 0.7)] += 1
    elif random_discount == ("car", 0.8):
        discount_customer_form[("car", 0.8)] += 1
    elif random_discount == ("car", 0.9):
        discount_customer_form[("car", 0.9)] += 1
    elif random_discount == ("house", 0.5):
        discount_customer_form[("house", 0.5)] += 1
    elif random_discount == ("house", 0.6):
        discount_customer_form[("house", 0.6)] += 1
    elif random_discount == ("house", 0.7):
        discount_customer_form[("house", 0.7)] += 1
    elif random_discount == ("house", 0.8):
        discount_customer_form[("house", 0.8)] += 1
    elif random_discount == ("house", 0.9):
        discount_customer_form[("house", 0.9)] += 1
    return discount_customer_form  #返回用户购物券清单


#退货函数，用户要有货才能退，打印出收到金额、余额，传入购物清单，传出余额和货物清单
def sales_return(commodity_inventory_form, remaining_money,commodity_form):
    goods = input("请输入想要退的商品：")
    if goods in commodity_inventory_form and commodity_inventory_form[goods] > 0:
        remaining_money += commodity_form[goods]
        commodity_inventory_form[goods] -= 1
        print(f"成功退款，到账：{commodity_form[goods]}元，余额：{remaining_money}元")
    else:
        print("不能退未拥有的商品")
    return commodity_inventory_form, remaining_money


#参数定义
money = 5000
#超市商品单
commodity = {
    "car":500,
    "house":1000
}

#用户持有折扣券
discount_customer = {("car", 0.5):0, ("car", 0.6):0, ("car", 0.7):0, ("car", 0.8):0, ("car", 0.9):0,
                     ("house", 0.5):0, ("house", 0.6):0, ("house", 0.7):0, ("house", 0.8):0, ("house", 0.9):0}

commodity_inventory = {}  #买到的商品清单

#购物循环
i = 'o'
while i != 'q':
    print("请输入要购买的商品，以空格为分隔:")
    str_input_goods= input()  #用户输入商品
    shopping_trolley = str_input_goods.split(" ")  #购物车

    discount_customer = coupon(discount_customer)  #抽奖,折扣券清单

    shopping_go = shopping(shopping_trolley, money, commodity, discount_customer, commodity_inventory)  #购物
    commodity_inventory = shopping_go[0]
    print(shopping_go[0])
    money = shopping_go[1]
    print(f"本次消费金额：{shopping_go[2]}")
    print(f"余额：{money}")
    print("是否继续购物？\n任意键继续\nq退出")
    i = input()

i = 'o'
#退货循环
while i != 'q':
    print("开始执行退货")
    goods_return = sales_return(commodity_inventory, money, commodity)
    commodity_inventory = goods_return[0]
    money = goods_return[1]
    print(f"余额：{money}")
    print("是否继续退货？\n任意键继续\nq退出")
    i = input()










